package com.zr.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zr.dao.PermissionDao;
import com.zr.dao.RoleDao;
import com.zr.dao.UserDao;
import com.zr.pojo.Permission;
import com.zr.pojo.Role;
import com.zr.pojo.User;
import com.zr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * @auther zhaorui
 * @date 2019-12-03 - 0:46
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;



    @Override
    public User findByUsername(String username) {
        User user  = userDao.findByUsername(username);
        if (user == null){
            return null;
        }

        //从数据表中获取role
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findRoleByUserId(userId);
        if (roles.size() > 0 && roles != null){
            for (Role role : roles) {

                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findPermissionById(roleId);
                role.setPermissions(permissions);
            }
        }
        user.setRoles(roles);
        return user;
    }
}
